package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.dto.CheckCustomDO;
import com.triones.ljr.entity.vo.BannerVO;
import com.triones.ljr.entity.vo.CheckCustomVO;
import com.triones.ljr.entity.vo.CustomCheckCustomVO;
import com.triones.ljr.mapper.CheckCustomMapper;
import com.triones.ljr.service.CheckCustomService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 检查制定
 */
@Service
@RequiredArgsConstructor
public class CheckCustomServiceImpl implements CheckCustomService {

    private final CheckCustomMapper checkCustomMapper;

    @Override
    public boolean add(CheckCustomDO dto) {
        return checkCustomMapper.insert(dto) > 0;
    }

    @Override
    public boolean checkTitleUnique(CheckCustomDO dto) {
        boolean exist = checkCustomMapper.exists(new LambdaQueryWrapper<CheckCustomDO>()
            .eq(CheckCustomDO::getTitle, dto.getTitle())
            .eq(CheckCustomDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), CheckCustomDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public TableDataInfo<CustomCheckCustomVO> pageList(CheckCustomDO dto, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("tcc.is_del", "0")
            .eq(ObjectUtil.isNotEmpty(dto.getDeptId()), "tcc.dept_id", dto.getDeptId())
            .like(ObjectUtil.isNotEmpty(dto.getTitle()), "tcc.title", dto.getTitle())
            .orderByAsc("tcc.sort")
            .orderByAsc("tcc.create_time");
        Page<CustomCheckCustomVO> result = checkCustomMapper.sysPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CustomCheckCustomVO> sysPageList(CheckCustomDO dto, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("tcc.is_del", "0")
            .orderByAsc("tcc.sort");
        Page<CustomCheckCustomVO> result = checkCustomMapper.sysPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public CustomCheckCustomVO getInfo(Long id) {
        return checkCustomMapper.getInfo(id);
    }

    @Override
    public boolean remove(Long id) {
        CheckCustomDO checkCustomDO = checkCustomMapper.selectById(id);
        if (checkCustomDO == null) {
            throw new ServiceException("数据不存在");
        }
        checkCustomDO.setIsDel("1");
        return checkCustomMapper.updateById(checkCustomDO) > 0;
    }

    @Override
    public boolean edit(CheckCustomDO dto) {
        return checkCustomMapper.updateById(dto) > 0;
    }
}
